﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using TTSModel;
using TTSBLL;
using TTS;
using System.Data;

public partial class DM_T0002053Right_Add : System.Web.UI.Page
{
    private Assemble a;
    private string message;
    private UnitType u;
    private const string kccDownPEC_2053 = "KCQR05AA";
    private IList<UnitTypeInfo> assembleVersionListT0002053;
    private string T0002053Right_SN { get { return ViewState["T0002053Right_SN"].ToString(); } set { ViewState["T0002053Right_SN"] = value; } }
    private string T0002053Right_KccUp_SN { get { return ViewState["T0002053Right_KccUp_SN"].ToString(); } set { ViewState["T0002053Right_KccUp_SN"] = value; } }
    private string T0002053Right_KccDown_SN { get { return ViewState["T0002053Right_KccDown_SN"].ToString(); } set { ViewState["T0002053Right_KccDown_SN"] = value; } }

    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }
    public DM_T0002053Right_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
            assembleVersionListT0002053 = u.LoadEntities("T0002053");
            this.T0002053Right_Version.Text = assembleVersionListT0002053[0].TcodeVersion + "," + assembleVersionListT0002053[1].TcodeVersion;
          //  this.T0002053Right_Version.Text = u.UnitType_GetbyTcode("T0002053").TcodeVersion;
        }
        this.TextBox_T0002053Right.Focus();
    }

    protected void TextBox_T0002053Right_TextChanged(object sender, EventArgs e)
    {
        T0002053Right_SN = this.TextBox_T0002053Right.Text.Trim();
        if (SNHelper.SNCheck(T0002053Right_SN, "T0002053", out message))
        {
            if (SNHelper.SNCheckVersionTwo(T0002053Right_SN, this.T0002053Right_Version.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002053Right_SN, "T0002053", out message))
                {
                    this.TextBox_T0002053Right.Enabled = false;
                    this.TextBox_T0002053RightKccUp.Enabled = true;
                    this.TextBox_T0002053RightKccUp.Focus();
                    return;
                }
            }

        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002053Right.Text = "";
        this.TextBox_T0002053Right.Focus();
    }
    protected void TextBox_T0002053RightKccUp_TextChanged(object sender, EventArgs e)
    {
        T0002053Right_KccUp_SN = this.TextBox_T0002053RightKccUp.Text.Trim();
        if (SNHelper.KccUpSNCheck(T0002053Right_KccUp_SN, out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002053Right_KccUp_SN, out message))
            {
                this.TextBox_T0002053RightKccUp.Enabled = false;
                this.TextBox_T0002053RightKccDown.Enabled = true;
                this.TextBox_T0002053RightKccDown.Focus();
                return;
            }
        }

        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002053RightKccUp.Text = "";
        this.TextBox_T0002053RightKccUp.Focus();
    }
    protected void TextBox_T0002053RightKccDown_TextChanged(object sender, EventArgs e)
    {
        T0002053Right_KccDown_SN = this.TextBox_T0002053RightKccDown.Text.Trim();
        if (SNHelper.KccDownSNCheck(T0002053Right_KccDown_SN, out message))
        {
            if (SNHelper.CheckKccDownPec(T0002053Right_KccDown_SN, kccDownPEC_2053, out message))
            {
                if (SNHelper.SNCheckKccDownVersion(T0002053Right_KccDown_SN, "01", out message))
                {
                    this.TextBox_T0002053RightKccDown.Enabled = false;
                    this.ConfirmSubmit.Enabled = true;
                    this.ConfirmSubmit.Focus();
                    return;
                }
            }

        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002053RightKccDown.Text = "";
        this.TextBox_T0002053RightKccDown.Focus();
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        if (ProConditionHelper.CheckMainSNsPrecondition(T0002053Right_SN, "T0002053", out message))
        {
            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002053Right_KccUp_SN, out message))
            {
                Unit u = new Unit();
                u.Insert(new UnitInfo() { SN = T0002053Right_SN, Tcode = "T0002053", IsLeaf = true, IsRoot = true });
                KCCSNUnion ku = new KCCSNUnion();
                KCCSNUnionInfo kui = new KCCSNUnionInfo() { KCCSN1 = T0002053Right_KccUp_SN, KCCSN2 = T0002053Right_KccDown_SN, SN = T0002053Right_SN, UserID = "111113" };
                ku.Insert(kui);

                AssembleInfo ainfo = new AssembleInfo()
                {
                    ID = 1,
                    SN = T0002053Right_SN,
                    IsRoot = true,
                    RecordTime = DateTime.Now,
                    Tcode = "T0002053",
                    Userid = "111116"
                };

                if (a.Assemble_Insert(ainfo, dt))
                {
                    message = "录入成功";

                    this.TextBox_T0002053Right.Text = "";
                    this.TextBox_T0002053RightKccUp.Text = "";
                    this.TextBox_T0002053RightKccDown.Text = "";
                    this.TextBox_T0002053Right.Enabled = true;
                    this.TextBox_T0002053Right.Focus();
                    this.ConfirmSubmit.Enabled = false;

                }
                else
                {
                    message = "录入失败，请联系相关人员";
                }
            }
        }
            
       
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("DM.aspx");
    }
}